package com.e.ego.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.ego.e.dialog.ELoading

/**
 * import kotlinx.android.synthetic.main.activity_kotlin.*
 * Created by lxy on 2017/5/28.
 */
abstract class EActivity : AppCompatActivity() {

    var loading: ELoading? = null

    abstract fun layout(): Int
    abstract fun onActivityCreated(state: Bundle?)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(layout())
        onActivityCreated(savedInstanceState)
    }

    fun loading(){
        if (loading == null){
            loading = ELoading(this)
        }
        if (!loading!!.isShowing){
            loading!!.loading()
        }
    }

    fun dismmis(){
        if (isFinishing) {
            loading = null
            return
        }
        if (loading !=null && loading!!.isShowing){
            loading!!.dismiss()
        }
    }

    fun to(clazz: Class<*>, extras: Bundle?) {
        val intent = Intent(this, clazz)
        if (extras != null) {
            intent.putExtras(extras)
        }
        startActivity(intent)
    }

    fun to(clazz: Class<*>) = to(clazz, null)

    fun to(action: String, extras: Bundle?) {
        val mIntent = Intent(action)
        if (extras != null) {
            mIntent.putExtras(extras)
        }
        startActivity(mIntent)
    }

    fun forResult(clazz: Class<*>, extras: Bundle?, requestCode: Int) {
        val intent = Intent(this, clazz)
        if (extras != null) {
            intent.putExtras(extras)
        }
        startActivityForResult(intent, requestCode)
    }

    fun forResult(clazz: Class<*>, requestCode: Int) = forResult(clazz, null, requestCode)

    fun Context.toast(toast: String) = Toast.makeText(this, toast, Toast.LENGTH_SHORT).show()

    fun Context.toast(toast: Int) = Toast.makeText(this, toast, Toast.LENGTH_SHORT).show()

    fun Context.longToast(toast: String) = Toast.makeText(this, toast, Toast.LENGTH_LONG).show()

    fun Context.longToast(toast: Int) = Toast.makeText(this, toast, Toast.LENGTH_LONG).show()

    fun hideKeybord() {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(this.window.decorView
                .windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    override fun onDestroy() {
        dismmis()
        loading = null
        super.onDestroy()
    }
}